<?php declare(strict_types=1);

namespace App\Api\V2\Controller\Income;

use App\Api\Controllers\BaseController;
use App\Exceptions\BizException;
use App\Services\Income\IncomeFactory;
use App\Traits\ResponseTrait;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Validation\Validator;

class IncomeController extends BaseController
{

    use ResponseTrait;


    public function incomeQuery(Request $request): string
    {
        $data = $request->all();
        $validator = \Illuminate\Support\Facades\Validator::make($data, [
            "store_id" => ["required"],
            "company" => ["required"],
        ], [], [
            "people" => "店铺ID",
            "company" => "通道类型",
        ]);
        if ($validator->fails()) {
            throw  new \Exception($validator->errors()->first());
        }
        try {
            $income = (new IncomeFactory())->getStrategy("gtxy");
            $result = $income->incomeQuery($data["store_id"], $data["company"]);
            return json_encode([
                "status" => 1,
                "data" => [
                    "status" => $result["status"]
                ],
                "message" => "ok",
            ]);
        } catch (\Exception $e) {
            return json_encode([
                'status' => 2,
                'message' => $e->getMessage()
            ]);
        }
    }

    public function incomeNotify(Request $request): string
    {
        $data = $request->all();
        try {
            Log::channel("income")->info("incomeNotify", $data);
            $income = (new IncomeFactory())->getStrategy("gtxy");
            $result = $income->incomeNotify($data);
        } catch (\Exception $e) {
            $result = json_encode([
                "rspCod" => "",
                "rspMsg" => $e->getMessage()
            ]);
        }
        return $result;
    }

}